﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.Helpers;
using NRCP.App_Service;
using System.Globalization;
using NRCP.BusinessEntities;

namespace NRCP
{
    public partial class Plan_Monitoring_Schedule : System.Web.UI.Page
    {
        List<Employee> emplyees = null;

        private Int32 TotalScheduleTobeDone
        {
            get
            {
                try
                {
                    return Convert.ToInt32(ViewState["TotalScheduleTobeDone"].ToString());
                }
                catch
                {
                    return 0;
                }
            }
            set
            {
                ViewState["TotalScheduleTobeDone"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadEmployee()
        {
            EmployeeBLL bll = new EmployeeBLL();
            emplyees=bll.GetAllEmployees();
        }
        private void LoadData()
        {
            NationalPlanBLL planbll = new NationalPlanBLL();
            NationalPlan plan=planbll.GetNationalPlanById(SiteUtility.IntQueryParam);
            if (plan != null)
            {
                SiteDataUtility utility = new SiteDataUtility();
                ddlRegion.DataSource = utility.LoadPlanRegion(SiteUtility.IntQueryParam);
                ddlRegion.DataTextField = "Key";
                ddlRegion.DataValueField = "Value";
                ddlRegion.DataBind();

                //var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.TakeWhile(m => m != String.Empty).Select((m, i) => new { Month = i + 1, MonthName = m }).ToList();
                ddlMonth.DataSource = utility.LoadPlanMonths(SiteUtility.IntQueryParam);
                ddlMonth.DataTextField = "Key";
                ddlMonth.DataValueField = "Value";
                ddlMonth.DataBind();                

                LoadEmployee();

                NationalPlanDetailsMonthlyBLL monthlyBll = new NationalPlanDetailsMonthlyBLL();
                TotalScheduleTobeDone=monthlyBll.GetNationalPlanDetailsMonthliesByNationalPlanId(SiteUtility.IntQueryParam).Count;
                hidPlanYear.Value = plan.PlanYear.ToString();
                respMessage.SetNoticeMessage("Monitoring Schedule for plan Year- "+plan.PlanYear);
            }
           
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            NationalPlanBLL planbll = new NationalPlanBLL();
            List<SampleSchedule>  schedules=planbll.GetMonitoringSchedule(SiteUtility.IntQueryParam, Convert.ToInt32(ddlRegion.SelectedItem.Value),Convert.ToInt32(ddlMonth.SelectedItem.Value));
            repMonitorSchedule.DataSource = schedules;
            repMonitorSchedule.DataBind();
            btnSubmit.Visible = true;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<Int32> completedSchedule=SessionManager.TEMP_ID_CONTAINER;

            TestSampleInformationBLL bll = new TestSampleInformationBLL();
            foreach (RepeaterItem item in repMonitorSchedule.Items)
            {
                DropDownList ddlInspector = item.FindControl("ddlInspector") as DropDownList;
                HiddenField hidSample = item.FindControl("hidSample") as HiddenField;
                HiddenField hidScheduleId = item.FindControl("hidScheduleId") as HiddenField;
                TextBox txtPlanDate = item.FindControl("txtPlanDate") as TextBox;
                if (ddlInspector != null && hidSample != null && hidScheduleId != null && txtPlanDate != null)
                {
                    
                    TestSampleInformation testInfo = null;
                    if (hidScheduleId.Value.Trim() != "-1")
                    {
                        testInfo = bll.GetTestSampleInformationById(Convert.ToInt32(hidScheduleId.Value.Trim()));
                    }
                    else
                    {
                        testInfo = new TestSampleInformation();
                    }

                    testInfo.NationalPlanDetailsMonthlyId = Convert.ToInt32(hidSample.Value.Trim());
                    testInfo.AssignedOn =Convert.ToDateTime(txtPlanDate.Text.Trim());
                    testInfo.AssignedTo = Convert.ToInt32(ddlInspector.SelectedItem.Value);
                    testInfo.Status = PlanStatus.Pending.ToString();
                    if (hidScheduleId.Value.Trim() != "-1")
                    {
                        bll.UpdateTestSampleInformation(testInfo);
                    }
                    else
                    {
                        bll.CreateNewTestSampleInformation(testInfo);
                    }

                    if (!completedSchedule.Contains(testInfo.NationalPlanDetailsMonthlyId))
                    {
                        completedSchedule.Add(testInfo.NationalPlanDetailsMonthlyId);
                    }
                }
            }

            if (TotalScheduleTobeDone > completedSchedule.Count)
                respMessage.SetSuccessMessage("Schedule done for month " + ddlMonth.SelectedItem.Text + " for region " + ddlRegion.SelectedItem.Text + ". Please complete rest of the schedule");
            else
                HttpContext.Current.Response.Redirect("~/plan-list.aspx");
        }

       

        protected void repMonitorSchedule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList ddlInspector = e.Item.FindControl("ddlInspector") as DropDownList;
                if (ddlInspector != null)
                {
                    if (emplyees == null)
                    {
                        LoadEmployee();
                    }

                    ddlInspector.DataSource = emplyees;                    
                    ddlInspector.DataBind();
                }
            }
        }
    }
}